package com.supermap.dataservice.file;

import cn.hutool.http.HttpUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dataservice.util.NearestPoint;
import com.supermap.dataservice.util.Point;
import com.supermap.dubbo.api.cloud.meto.MetoService;
import lombok.SneakyThrows;
import org.apache.logging.log4j.util.Strings;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author aqua
 * @description
 * @date 2019年12月31 17:22
 * @email 1301584033@qq.com
 */
@Service
public class MetoServiceImpl implements MetoService {

    @Resource
    private StationMapper stationMapper;

    @Override
    public JSONUtils getFrecastDataList(String areaCode, String lat, String lon) {
        List<Point> pointList= stationMapper.getAll();
        return  new JSONUtils().setData(stationMapper.getFrecastDataList(getStationNo(areaCode,lat,lon,pointList)));
    }

    @Override
    public String getWeatherDataList(String areaCode, String lat, String lon) {
        List<Point> pointList= stationMapper.getAll();
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("param", "{'Function':'gethourWeatherbystation','CustomParams':{station:'"+getStationNo(areaCode,lat,lon,pointList)+"'},'Type':2}");
        String result= HttpUtil.post("http://183.230.183.18:8088/SPPMDataService/services/data/query",paramMap);
        return  result;
    }

    @Override
    @SneakyThrows
    public JSONUtils getSurfchndayList(String areaCode, String lat, String lon) {
        List<Point> pointList= stationMapper.getAll();
       return  new JSONUtils().setData(stationMapper.getSurfchndayList(getStationNo(areaCode,lat,lon,pointList)));
    }

    @Override
    public JSONUtils getPubertyDate(String crop) {
        return  new JSONUtils().setData(stationMapper.getPubertyDate(crop));
    }

    @Override
    public JSONUtils getYearPubertyDate(String crop) {
        return  new JSONUtils().setData(stationMapper.getYearPubertyDate(crop));
    }

    @Override
    public JSONUtils getGridProduct(String areaCode, String data, String gridCode) {
        return  new JSONUtils().setData(stationMapper.getGridProduct(areaCode,data,gridCode));
    }

    @Override
    public JSONUtils getVendorList(String areaCode) {
        return  new JSONUtils().setData(stationMapper.getVendorList(areaCode));
    }

    @Override
    public String getGridList(String areaCode, String lat, String lon, String date) {
        List<Point> pointList= stationMapper.getSoilAll();
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("param", "{'Function':'getsoilmulday','CustomParams':{stationid:'"+getStationNo(areaCode,lat,lon,pointList)+"',date_day:'"+date+"'},'Type':2}");
        String result= HttpUtil.post("http://183.230.183.18:8088/SPPMDataService/services/data/query",paramMap);
        return  result;
    }

    @Override
    public JSONUtils getGoodProduct(String areaCode) {
        return  new JSONUtils().setData(stationMapper.getGoodProduct(areaCode));
    }

    @Override
    public JSONUtils getFiremonitor(String areaCode, String date) {
        return  new JSONUtils().setData(stationMapper.getFiremonitor(areaCode,date));
    }

    @Override
    public JSONUtils getDisasteRiskType() {
        return  new JSONUtils().setData(stationMapper.getDisasteRiskType());
    }

    @Override
    public JSONUtils getClimaticResourcesType() {
        return  new JSONUtils().setData(stationMapper.getClimaticResourcesType());
    }

    @Override
    public JSONUtils get5DayForcast() {
        return  new JSONUtils().setData(stationMapper.get5DayForcast());
    }

    @Override
    public JSONUtils getLifeInfo(String areaCode, String lat, String lon) {
        List<Point> pointList= stationMapper.getAll();
        String stationId= getStationNo(areaCode,lat,lon,pointList);
        return  new JSONUtils().setData(stationMapper.getLifeInfo(stationId));
    }

    @Override
    public String get5DayHistory() {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("param", "{'Function':'get5DayHistory','CustomParams':{},'Type':2}");
        String result= HttpUtil.post("http://183.230.183.18:8088/SPPMDataService/services/data/query",paramMap);
        return  result;
    }

    @SneakyThrows
    public String getStationNo(String areaCode, String lat,String lon,List<Point> pointList){
        if(Strings.isNotBlank(lon)&&Strings.isNotBlank(lat)){
            return  NearestPoint.getNearestStationNo(Double.parseDouble(lon),Double.parseDouble(lat),pointList);
        }else{
            String xy[]=stationMapper.getAreaCode(areaCode).split(",");
            return  NearestPoint.getNearestStationNo(Double.parseDouble(xy[0]),Double.parseDouble(xy[1]),pointList);
        }
    }

    @Override
    public JSONUtils getOneMapSecondMenuType(String code) {
        String tcode = "";
        /* 1.参数错误验证
           2.前期接口修改，增加判断逻辑，增加容错性
        */

        if(Strings.isBlank(code))
        {
            return  new JSONUtils().setMsg("获取一张图二级数据菜单【getOneMapSecondMenuType】," +
                    "参数错误【code:"+code +"】,请确认参数！");
        }
        else if("20".equals(code))
        {
            tcode = "A02004";
        }
        else if("21".equals(code))
        {
            tcode = "A02005";
        }
        else if("24".equals(code))
        {
            tcode = "A02015";
        }
        else if("25".equals(code))
        {
            tcode = "A02016";
        }
        else
        {
            tcode = code;
        }

        return  new JSONUtils().setData(stationMapper.getOneMapSecondMenu(tcode));
    }

}
